<template>
  <div style="margin-top:20px">
    <el-table border ref="multipleTable" height="520" :data="rule_map" size='mini'>
      <template slot="empty">
        <el-empty :description="$t('暂无数据')"></el-empty>
      </template>
      <el-table-column prop="rule_id" align='center' :label="$t('规则ID')" width="80" />
      <el-table-column prop="proto" align='center' :formatter="protoValue" width="120" :label="$t('协议名称')">
        <template slot-scope="scope">
          {{ rule[scope.row.proto] }}
        </template>
      </el-table-column>
      <el-table-column prop="action" align='center' :formatter="matchOptionFormatter" width="80" :label="$t('动作')" />
      <el-table-column prop="commond" align='center' :formatter="commondFormatter" width="140" :label="$t('功能码')" />
      <el-table-column prop="accept_range" align='center' :formatter="acceptRangeFormatter" width="100" :label="$t('匹配范围')" />
      <el-table-column prop="start_addr" align='center' min-width="100" :label="$t('起始地址')" />
      <el-table-column prop="end_addr" align='center' min-width="100" :label="$t('结束地址')" />
      <el-table-column align="center" :label="$t('操作')" width="160" fixed='right'>
        <template slot-scope="scope">
          <el-button type="success" plain @click="editRule(scope.row)" size='mini' :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
          <el-button type="danger" plain @click="deleteRule(scope.row)" size='mini' :class="'del' +(scope.$index+1)">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import ruleArray from '../../ruleArray'
export default {
  name: 'CipmbTable',
  props: {
    ruleMap: {
      type: Array,
      default () {
        return []
      }
    }
  },
  watch: {
    ruleMap: {
      handler (newValue, oldValue) {
        this.rule_map = []
        newValue.forEach(val => {
          if (val.proto === 'cipmb') {
            this.rule_map.push(val)
          }
        })
      },
      deep: true // 深度监听
    }
  },
  data () {
    return {
      rule: ruleArray.ruleShow,
      rule_map: [],
      commondList: ruleArray.commondList,
      rangeList: ruleArray.rangeList,
      actionList: { 'accept': '允许', 'alert': '告警' },
      acceptRangeList: { 'all': '所有', 'single': '单个' }
    }
  },
  mounted () {
    this.rule_map = []
    this.ruleMap.forEach(val => {
      if (val.proto === 'cipmb') {
        this.rule_map.push(val)
      }
    })
  },
  methods: {
    // 删除规则
    deleteRule (row) {
      this.$emit('delRuleMap', row)
    },
    // 编辑规则
    editRule (row) {
      this.$emit('largeConfig', row)
    },
    // 协议名称显示
    protoValue (data) {
      return this.rule[data.proto]
    },
    // 动作格式化
    matchOptionFormatter (data) {
      return this.$t(this.actionList[data.action])
    },
    // cipmb功能码格式化
    commondFormatter (data) {
      return this.$t(this.commondList[data.commond]) || data.commond
    },
    // cipmb匹配范围格式化
    acceptRangeFormatter (data) {
      return this.$t(this.acceptRangeList[data.accept_range])
    }
  }
}
</script>
